import requests
import json


def get_weather(city):
    url = 'http://jisutqybmf.market.alicloudapi.com/weather/query'
    appcode = '9e974f33ce9c44509cf434c07a24f788'
    headers = {'Authorization': 'APPCODE ' + appcode}
    try:
        content = requests.get(url=url, params={'ip': str(city)}, headers=headers)
        print(content.json())
        return content.json()
    except requests.exceptions.Timeout:
        return 'TimeoutError'
    except requests.exceptions.ConnectionError:
        return 'ConnectionError'
    except requests.exceptions.HTTPError:
        return 'HTTPError'
    except requests.exceptions.TooManyRedirects:
        return 'TooManyRedirects'
    except:
        return 'OtherError'



def main():
    ip_address = requests.get("http://ip.42.pl/raw").text
    print(ip_address)
    json_weather = get_weather(ip_address)
    return json_weather['result']
    # print(len(json_weather))
    # res = json_weather['result']
    # print(res['city'])
    # print(res['date'])
    # print(res['week'])
    # print(res['weather'])
    # print(res['temp'])
    # print(res['index'][3]['detail'])
    # print(type(json_weather))


if __name__ == '__main__':
    main()